import { ReactViewState, ReactView, reactControl, ReactViewControl } from "pao-aop-client";
import { ROUTE_PATH } from "src/projects/router";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import React from "react";
import { addon } from "pao-aop";
import { AppServiceUtility } from "src/projects/app/appService";
import { Row, Modal, message, DatePicker, Col, Select } from "antd";
import { request } from "src/business/util_tool";
import { WhiteSpace } from "antd-mobile";
const Option = Select.Option;
/**
 * 组件：活动管理列表状态
 */
export interface ActivityPublishViewState extends ReactViewState {
    /** 接口名 */
    request_url?: string;
    // 是否显示
    modal_visible: boolean;
    // 当前选择的活动
    selected_activity: any;
    // 选择的推送时间
    selected_date: any;
    // 带参
    select_type?: any;
    // 活动类型
    activity_type: any;
}

/**
 * 组件：活动管理列表
 * 描述
 */
export class ActivityPublishView extends ReactView<ActivityPublishViewControl, ActivityPublishViewState> {
    private columns_data_source = [{
        title: '活动名称',
        dataIndex: 'activity_name',
        key: 'activity_name',
    }, {
        title: '商家',
        dataIndex: 'organization_name',
        key: 'organization_name',
    }, {
        title: '活动场地',
        dataIndex: 'address',
        key: 'address',
    }, {
        title: '活动类型',
        dataIndex: 'activity_type_name',
        key: 'activity_type_name',
    }, {
        title: '开始时间',
        dataIndex: 'begin_date',
        key: 'begin_date',
    }, {
        title: '结束时间',
        dataIndex: 'end_date',
        key: 'end_date',
    }, {
        title: '服务费用',
        dataIndex: 'amount',
        key: 'amount',
    }, {
        title: '审核状态',
        dataIndex: 'status',
        key: 'status',
    },
        // {
        //     title: '活动二维码',
        //     dataIndex: 'qr_code',
        //     key: 'qr_code',
        // }
    ];
    private table_params = {
        other_label_type: [{ type: 'icon', label_key: 'icon_edit', label_parameter: { icon: 'antd@edit' } }],
        showHeader: true,
        bordered: false,
        show_footer: true,
        rowKey: 'id',
    };
    constructor(props: ActivityPublishViewControl) {
        super(props);
        this.state = {
            modal_visible: false,
            selected_activity: [],
            selected_date: "",
            activity_type: [],
            select_type: this.props.select_param && this.props.select_param['type'] ? this.props.select_param['type'] : '',
        };
    }
    /** 新增按钮 */
    add = () => {
        this.props.history!.push(ROUTE_PATH.changeActivityManage);
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        const { select_type } = this.state;
        if ('icon_edit' === type) {
            // 来自活动所有列表
            if (select_type === 'sh') {
                this.props.history!.push(ROUTE_PATH.viewActivityManage + '/' + contents.id);
            } else {
                this.props.history!.push(ROUTE_PATH.changeActivityManage + '/' + contents.id + (contents.hasOwnProperty('step_no') && contents['step_no'] === 1 ? '/change' : ''));
            }
        } else if ('icon_align_center' === type) {
            this.setState({
                modal_visible: true,
                selected_activity: contents,
            });
        }
    }
    componentDidMount() {
        // 获取活动类型
        request(this, AppServiceUtility.activity_service.get_activity_type_list!({}, 1, 99)!)
            .then((datas: any) => {
                if (datas && datas['result']) {
                    this.setState({
                        activity_type: datas['result'],
                    });
                }
            }).catch((error: Error) => {
                message.error(error.message);
            });
    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
        });
    }
    handleOk() {
        const { selected_activity, selected_date } = this.state;
        if (selected_date === '') {
            message.error(`请选择推送时间！`);
            return;
        }
        let param = {
            message_name: selected_activity.activity_name,
            message_content: selected_activity.introduce,
            message_type: 'f88d178c-10f5-11ea-869d-a0a4c57e9ebe',
            business_type: 'activity',
            business_id: selected_activity.id,
            remark: '活动推送',
            push_date: selected_date,
        };
        // 获取信息类型
        request(this, AppServiceUtility.message_service.add_new_message!(param)!)
            .then((datas: any) => {
                if (datas === 'Success') {
                    message.success('推送成功！');
                    this.handleCancel();
                } else {
                    message.error(`错误信息：${datas}`);
                }
            }).catch((error: Error) => {
                message.error(error.message);
            });
    }
    handleCancel() {
        this.setState({
            modal_visible: false,
        });
    }
    onPushDateChange(value: any, dateString: any) {
        this.setState({
            selected_date: value,
        });
    }
    onPushDateOk(value: any) {
        this.setState({
            selected_date: value,
        });
    }
    render() {
        let select_param = this.props.select_param;
        let param = {};
        if (select_param.hasOwnProperty('type')) {
            param['all'] = select_param['type'];
        }
        let service_item = {
            type_show: false,
            edit_form_items_props: [{
                type: InputType.input,
                label: "活动名称",
                decorator_id: "activity_name",
                option: {
                    placeholder: "请输入活动名称",
                    // childrens: user_list,
                }
            }, {
                type: InputType.select,
                label: "活动类型",
                decorator_id: "activity_type_id",
                option: {
                    placeholder: "请选择活动类型",
                    childrens: this.state.activity_type!.map((item: any) => <Option key={item.id} value={item.id}>{item.name}</Option>),
                }
            }],
            btn_props: [{
                label: '活动发布',
                btn_method: this.add,
                icon: 'plus'
            }],
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.activity_service,
            service_option: {
                select: {
                    service_func: this.state.request_url,
                    service_condition: [param, 1, 10]
                },
                delete: {
                    service_func: 'del_activity'
                }
            },
            searchExtraParam: param,
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.select_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        // 推送列表用推送按钮代替
        if (select_param['type'] && select_param['type'] === 'ts') {
            this.table_params.other_label_type = [{ type: 'icon', label_key: 'icon_align_center', label_parameter: { icon: 'antd@align-center' } }];
        }
        let service_item_list = Object.assign(service_item, this.table_params);
        const { selected_activity, modal_visible } = this.state;
        return (
            <Row>
                <Modal
                    title="活动推送"
                    visible={modal_visible}
                    onOk={() => this.handleOk()}
                    onCancel={() => this.handleCancel()}
                >
                    <Row type="flex" justify="center" align="middle">
                        <Col span={6}>
                            是否推送活动：
                        </Col>
                        <Col span={18}>
                            {selected_activity.activity_name}
                        </Col>
                    </Row>
                    <WhiteSpace />
                    <WhiteSpace />
                    <Row type="flex" justify="center" align="middle">
                        <Col span={6}>
                            推送时间：
                        </Col>
                        <Col span={18}>
                            <DatePicker showTime={true} placeholder="选择推送时间" onChange={(value: any, string: any) => this.onPushDateChange(value, string)} onOk={(value: any) => this.onPushDateOk(value)} />
                        </Col>
                    </Row>
                </Modal>
                <SignFrameLayout {...service_item_list} />
            </Row>
        );
    }
}

/**
 * 活动管理列表页面
 */
@addon('ActivityPublishView', '活动管理列表页面', '描述')
@reactControl(ActivityPublishView, true)
export class ActivityPublishViewControl extends ReactViewControl {
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
    /** 带参 */
    public select_param?: any = {};
} 